package com.yuntsg.ruiijn.controller.search;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuntsg.ruiijn.databse_search.entity.CustomizationPic;
import com.yuntsg.ruiijn.databse_search.service.CustomizationPicService;
import common.bean.R;
import common.bean.RRException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("manager/pic/alternative")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@SuppressWarnings("all")
@Slf4j
public class AlternativePicController {


    private final CustomizationPicService customizationPicService;


    /**
     * 备选列表
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("records/{rid}")
    @ResponseBody
    public R records(@PathVariable("rid") Integer rid) {
        List<CustomizationPic> list = customizationPicService.list(Wrappers.<CustomizationPic>lambdaQuery().eq(CustomizationPic::getRid, rid));
        return R.ok().put("list", list);
    }

    /**
     * 添加到备选
     *
     * @param content
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public R add(@RequestBody CustomizationPic data) throws IOException {
        if (null == data.getRid()) {
            throw new RRException("rid不能为空");
        }
        customizationPicService.saveOrUpdate(data);
        return R.ok();
    }

    /**
     * 删除备选
     *
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    @ResponseBody
    public R delete(@PathVariable("id") Integer id) {
        customizationPicService.removeById(id);
        return R.ok();
    }


}